package com.chen.lifepic.GrideView;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.chen.lifepic.DataInfo.DataInfo;
import com.chen.lifepic.downloader.BaseTask;
import com.chen.lifepic.downloader.DownloadManager;
import com.chen.lifepic.downloader.ITaskListener;
import com.chen.lifepic.utils.APPCommonDef.PictureType;
import com.chen.lifepic.utils.Utils;

public class DecodeImageTask extends BaseTask {
	protected Bitmap _bitmap = null;

	public DecodeImageTask(DataInfo info, ITaskListener listener,
			PictureType type) {
		super(info, listener, type);
	}

	public Bitmap getImage() {
		return _bitmap;
	}

	@Override
	public void start() {
		super.start();
		if (null != getMediaInfo())
			getImageAndDownloadIfNeeded();
	}

	protected void getImageAndDownloadIfNeeded() {
		if (null == getMediaInfo())
			return;

		String filePath = getMediaInfo().getDownloadPath(_pictureType);

		if (null == filePath)
			return;

		if (!Utils.isFileExist(filePath)) {
			DownloadManager.getInstance().addTaskToDownloadQueue(
				getMediaInfo(), _pictureType);
			return;
		}

		switch (_pictureType) {
		case PictureTypeThumbnail:
			_bitmap = decodeThumbnail(filePath, 300, 300);
			break;
		case PictureTypeScreennail:
			_bitmap = decodeThumbnail(filePath, 1000, 1000);
			break;

		case PictureTypeThumbnailLarge:
			_bitmap = decodeThumbnail(filePath, 400, 400);
			break;
		default:
			break;
		}

		if (null != _listener) {
			_listener.TaskFinish(this);
		}
	}

	Bitmap decodeThumbnail(String filePath, int width, int height) {
		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filePath, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, width, height);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(filePath, options);
	}

	Bitmap decodeOriginal(String filePath) {
		return BitmapFactory.decodeFile(filePath);
	}

	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {

			// Calculate ratios of height and width to requested height and
			// width
			final int heightRatio = Math.round((float) height
					/ (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);

			// Choose the smallest ratio as inSampleSize value, this will
			// guarantee
			// a final image with both dimensions larger than or equal to the
			// requested height and width.
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}

		return inSampleSize;
	}

}
